package file

import (
	"io/ioutil"
	"os"
)

// TextRead 读取文本文件
func TextRead(source string) ([]byte, error) {
	return ioutil.ReadFile(source)
}

// TextReadLine 按行读取文本文件
func TextReadLine(source string, handle func([]byte) error) error {
	file, err := os.Open(source)
	if err != nil {
		return err
	}
	defer file.Close()
	return ReaderLine(file, handle)
}

// TextWrite 写入文件
func TextWrite(source string, data []byte) error {
	return ioutil.WriteFile(source, data, os.ModePerm)
}

// TextAppend 追加写入文件
func TextAppend(source string, data []byte) error {
	file, err := os.OpenFile(source, os.O_WRONLY|os.O_CREATE, os.ModePerm)
	if err != nil {
		return err
	}
	defer file.Close()
	line, err := file.Seek(0, 2)
	if err != nil {
		return err
	}
	_, err = file.WriteAt(data, line)
	return err
}
